<?
require_once(BASE_DIR.'/lib/phpmailer/class.phpmailer.php');
require_once(BASE_DIR.'/lib/phpmailer/class.smtp.php');

class SmtpService
{
	static function Send($email, $name, $subject, $body)
	{
		if(HOST_TYPE=='local') return true;
		
		// заменяем спецсимволы
		$body = str_replace('&quot;','"',$body);
		$body = str_replace('&laquo;','"',$body);
		$body = str_replace('&raquo;','"',$body);
		$body = str_replace('&lsquo;',"'",$body);
		$body = str_replace('&lt;',"<",$body);
		$body = str_replace('&gt;',">",$body);
		$body = str_replace('&amp;',"&",$body);
		
		$mail             = new PHPMailer();
		$mail->IsSMTP();
		$mail->Host       = SMTP_HOST;
		$mail->Hostname   = SMTP_HOSTNAME;
		$mail->SMTPAuth   = true;
		$mail->Username   = SMTP_USER;
		$mail->Password   = SMTP_PASSWORD;
		$mail->CharSet    = "Windows-1251";
		$mail->SetFrom    (MAIL_FROM, MAIL_FROM_NAME);
		$mail->AddReplyTo (MAIL_NO_REPLY, MAIL_FROM_NAME);
		
		$mail->Subject    = $subject;
		$mail->Body  			= $body;
		if($name)
			$mail->AddAddress($email, $name);
		else
			$mail->AddAddress($email);
		
		if($mail->Send()) return true;
		else
		{
			// todo: вставляем в queue
			return false;
		}
	}
	
	
	static function SendHtml($email, $name, $subject, $body)
	{
		if(HOST_TYPE=='local') return true;
		
		try{
			$mail             = new PHPMailer();
			$mail->IsSMTP();
			$mail->Host       = SMTP_HOST;
			$mail->Hostname   = SMTP_HOSTNAME;
			$mail->SMTPAuth   = true;
			$mail->Username   = SMTP_USER;
			$mail->Password   = SMTP_PASSWORD;
			$mail->CharSet    = "Windows-1251";
			$mail->SetFrom    (MAIL_FROM, MAIL_FROM_NAME);
			$mail->AddReplyTo (MAIL_NO_REPLY, MAIL_FROM_NAME);
			
			$mail->Subject    = $subject;
			$mail->MsgHTML($body);
			$mail->AddAddress($email, $name);
			
			return $mail->Send();
		}
		catch (phpmailerException $e) 
		{
			return false;
		}
	}
}